﻿using UnityEngine;
using System.Collections;
using System;
using CHFramework;

public class AnimationController : MonoBehaviour
{
    [Serializable]
    public class AnimationData
    {
        public AnimationClip animation;
        public float speedAniamtion = 1f;
    }

    public AnimationData Run, TurnLeft, TurnRight, JumpUp, JumpLoop, JumpDown, Roll, Dead;

    public delegate void AnimationHandle();
    public AnimationHandle AnimationState;

    private Controller Controller;
    private float RunSpeed;
    private float DefaultRunSpeed;
    private Animation Animation;

    private void Awake()
    {
        this.Controller = GetComponent<Controller>();
        this.Animation = GetComponent<Animation>();
        this.DefaultRunSpeed = GameAttribute.Instance.Speed;
        this.AnimationState = OnRun;
    }

    private void Update()
    {
        if (this.AnimationState != null)
            this.AnimationState();
    }

    /// <summary>
    /// 跑动状态
    /// </summary>
    public void OnRun()
    {
        this.Animation.Play(Run.animation.name);
        this.RunSpeed = (GameAttribute.Instance.Speed / DefaultRunSpeed) * Run.speedAniamtion;
        this.Animation[Run.animation.name].speed = RunSpeed;
    }

    /// <summary>
    /// 跳起状态
    /// </summary>
    public void OnJump()
    {
        this.Animation.Play(JumpUp.animation.name);
        if (this.Animation[JumpUp.animation.name].normalizedTime > 0.95f)
            AnimationState = OnJumpLoop;
    }

    /// <summary>
    /// 二连跳
    /// </summary>
    public void OnJumpSecond()
    {
        this.Animation.Play(Roll.animation.name);
        if (this.Animation[Roll.animation.name].normalizedTime > 0.95f)
            AnimationState = OnJumpLoop;
    }

    /// <summary>
    /// 如果角色在空中还未落地，就一直播放跳起的动画
    /// </summary>
    public void OnJumpLoop()
    {
        this.Animation.CrossFade(JumpLoop.animation.name);
        if (Controller.CharacterController.isGrounded)
            AnimationState = OnRun;
    }

    /// <summary>
    /// 左移状态
    /// </summary>
    public void OnTurnLeft()
    {
        this.Animation.Play(TurnLeft.animation.name);
        this.Animation[TurnLeft.animation.name].speed = TurnLeft.speedAniamtion;
        if (this.Animation[TurnLeft.animation.name].normalizedTime > 0.95f)
            AnimationState = OnRun;
    }

    /// <summary>
    /// 右移状态
    /// </summary>
    public void OnTurnRight()
    {
        this.Animation.Play(TurnRight.animation.name);
        this.Animation[TurnRight.animation.name].speed = TurnRight.speedAniamtion;
        if (this.Animation[TurnRight.animation.name].normalizedTime > 0.95f)
            AnimationState = OnRun;
    }

    /// <summary>
    /// 滚动状态
    /// </summary>
    public void OnRoll()
    {
        this.Animation.Play(Roll.animation.name);
        if (this.Animation[Roll.animation.name].normalizedTime > 0.95f)
        {
            Controller.IsRoll = false;
            AnimationState = OnRun;
        }
        else
            Controller.IsRoll = true;
    }

    /// <summary>
    /// 死亡状态
    /// </summary>
    public void OnDead()
    {
        this.Animation.Play(Dead.animation.name);
    }
}
